package com.jule.teenpatti.mail.network.protocol.reqs;

import JoloProtobuf.AuthSvr.JoloAuth;
import com.jule.teenpatti.base.enums.ErrorCodeEnum;
import com.jule.teenpatti.mail.network.protocol.ClientReq;
import com.jule.teenpatti.mail.network.protocol.acks.JoloAuth_RedDotAck_600020;
import com.jule.teenpatti.mail.service.MailService;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
@Slf4j
public class JoloAuth_RedDotReq_600020 extends ClientReq {

    public JoloAuth_RedDotReq_600020(int functionId) {
        super(functionId);
    }
    private JoloAuth.JoloAuth_RedDotReq req;
    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloAuth.JoloAuth_RedDotReq.parseFrom(blob);
    }

    @Override
    public void processImpl() throws Exception {
        log.debug("收到消息-> " + functionId + ", reqNum-> " + header.reqNum + ", req->" + req.toString());
        String userId = req.getUserId();
        JoloAuth.JoloAuth_RedDotAck.Builder ack = JoloAuth.JoloAuth_RedDotAck.newBuilder();
        ack.setUserId(userId).setResult(1);
        try{

            List<JoloAuth.JoloAuth_MailItem> mails= MailService.getInstance().getMailListByUserId(userId);

            List<JoloAuth.JoloAuth_RedDotInfo> redDots = new ArrayList<>();

            JoloAuth.JoloAuth_RedDotInfo.Builder redDotInfo = JoloAuth.JoloAuth_RedDotInfo.newBuilder();
            log.debug("当前邮件的数量："+mails.size());
            redDotInfo.setType(1);
            redDotInfo.setValue(mails.size()>0?1:0);
            redDots.add(redDotInfo.build());

            ack.addAllRedDots(redDots);

            ctx.writeAndFlush(new JoloAuth_RedDotAck_600020(ack.build(),header));
        }catch(Exception ex){
            ack.setResult(0);
            ack.setResultMsg(ErrorCodeEnum.GAME_50002_2.getCode());
            ctx.writeAndFlush(new JoloAuth_RedDotAck_600020(ack.build(),header));

            log.error(ex.getMessage(),ex);
        }

    }
}
